package com.xiaoxu.bbs.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoxu.bbs.DAO.TopicDAO;
import com.xiaoxu.bbs.entity.Reply;
import com.xiaoxu.bbs.entity.Topic;
import com.xiaoxu.bbs.service.ReplyService;
import com.xiaoxu.bbs.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class TopicServiceImpl extends ServiceImpl<TopicDAO, Topic> implements TopicService {
    @Autowired
    private ReplyService replyService;
    @Override
    public Boolean deleteTopicWithItReplys(Long toipcId) {
        LambdaQueryWrapper<Reply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Reply::getTopicId,toipcId);
        List<Reply> replyList = replyService.list(queryWrapper);
        List<Long> idList = replyList.stream().map(Reply::getId).collect(Collectors.toList());
        if (idList.size() != 0) {
            for (Long id:idList) {
                deleteTopicWithItReplys(id);
            }
            replyService.removeByIds(idList);
        }
        return this.removeById(toipcId);
    }
}
